﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class InvoiceOther
    {
        private ISessionFactory _sessionFactory;
        public InvoiceOther()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.InvoiceOther)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListByDate(DateTime datefrom, DateTime dateto)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from Invoice_Other where DATE_OF_INVOICE between ? and ? order by DATE_OF_INVOICE").AddEntity(typeof(DataObjects.InvoiceOther));
                query.SetDateTime(0, datefrom);
                query.SetDateTime(1, dateto);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.InvoiceOther GetObjectByPK(string invoice_no)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Invoice_Other where Invoice_No =?").AddEntity(typeof(DataObjects.InvoiceOther));
                query.SetString(0, invoice_no);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.InvoiceOther)arrTemp[0];
                }
                return null;
            }
        }

        public int GetMaxID(string cond)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from Invoice_Other where Invoice_No like N'" + cond + "%' order by Invoice_No desc").AddEntity(typeof(DataObjects.InvoiceOther));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                DataObjects.InvoiceOther oInv;

                if (arrTemp.Count > 0)
                {
                    oInv = (DataObjects.InvoiceOther)arrTemp[0];
                    if (oInv != null)
                    {
                        string no = oInv.InvoiceNo.Replace(cond, "");
                        return int.Parse(no);
                    }
                }
                return 0;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.InvoiceOther oInvoice)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oInvoice);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.InvoiceOther oInvoice)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oInvoice);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string invoice_no)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.InvoiceOther oInvoice = (DataObjects.InvoiceOther)session.Load(typeof(DataObjects.InvoiceOther), invoice_no);
                session.Delete(oInvoice);
                tx.Commit();
            }
        }
    }
}
